home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg1 / vulprog1.c < prev   
Encoding:
C/C++ Source or Header  |  1999-01-05  |  1.6 KB  |  59 lines

  1. /*
  2.  * Copyright (C) January 1999, Matt Conover & w00w00 Security Development
  3.  *
  4.  * This is a typical vulnerable program.  It will store user input in a
  5.  * temporary file.  argv[1] of the program is will have some value used
  6.  * somewhere else in the program.  However, we can overflow our user input
  7.  * string (i.e. the gets()), and have it overwrite the temporary file
  8.  * pointer, to point to argv[1] (where we can put something such as
  9.  * "/root/.rhosts", and after our garbage put a '#' so that our overflow
  10.  * is ignored in /root/.rhosts as a comment).  We'll assume this is a
  11.  * setuid program.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <errno.h>
  19.  
  20. #define ERROR -1
  21. #define BUFSIZE 16
  22.  
  23. /* 
  24.  * Run this vulprog as root or change the "vulfile" to something else.
  25.  * Otherwise, even if the exploit works it won't have permission to
  26.  * overwrite /root/.rhosts (the default "example").
  27.  */
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.    FILE *tmpfd;
  32.    static char buf[BUFSIZE], *tmpfile;
  33.  
  34.    if (argc <= 1)
  35.    {
  36.       fprintf(stderr, "Usage: %s <garbage>\n", argv[0]);
  37.       exit(ERROR);
  38.    }
  39.  
  40.    tmpfile = "/tmp/vulprog.tmp"; /* no, this is no a temp file vul */
  41.    printf("before: tmpfile = %s\n", tmpfile);
  42.  
  43.    /* okay, now the program thinks that we have access to argv[1] */
  44.    printf("Enter one line of data to put in %s: ", tmpfile);
  45.    gets(buf);
  46.  
  47.    printf("\nafter: tmpfile = %s\n", tmpfile);
  48.  
  49.    tmpfd = fopen(tmpfile, "w");
  50.    if (tmpfd == NULL)
  51.    {
  52.       fprintf(stderr, "error opening %s: %s\n", tmpfile, strerror(errno));
  53.       exit(ERROR);
  54.    }
  55.  
  56.    fputs(buf, tmpfd);
  57.    fclose(tmpfd);
  58. }
  59.